<?php

class TaxonomyController extends Controller {
    /**
     * 
     */
    public function filters() {
        parent::filters();
        $this->menu_backend = "#cms";
    }

    /**
     *  list Taxonomy
     */
    public function actionIndex() {
        
        $Taxonomy = Taxonomy::model()->findByPk(1);
        $descendants = $Taxonomy->descendants()->findAll();
        $this->render('index');
    }

    /**
     * 
     */
    public function actionEdit($id = null) {
        if ($id != null && $id != 1) {
            $model = Taxonomy::model()->findByPk($id);
            if ($model != null) {
                if (isset($_POST['Taxonomy'])) {
                    $parent = Taxonomy::model()->findByPk($_POST['Taxonomy']['parents']);
                    if ($parent != null) {
                        $model->attributes = $_POST['Taxonomy'];
                        $image = CUploadedFile::getInstance($model, 'image');
                        if ($image != null) {
                            $newName = time() . "." . $image->extensionName;
                            $model->image = $newName;
                        }
                        if ($model->save() && $model->moveAsFirst($parent)) {
                            if($image != null && isset($newName)){
                                 $image->saveAs(PATH_IMAGE_PRODUCT . $newName);
                            }
                            Yii::app()->user->setFlash('success', "Danh mục sản phẩm đã được lưu");
                            $this->redirect(array('Taxonomy/index'));
                        }
                    }
                }
                $listTaxonomy = Taxonomy::getListTaxonomy(1, $id);
                $this->render('add', array('model' => $model, 'listTaxonomy' => $listTaxonomy, 'id' => $id));
            } else {
                throw new CHttpException(404, 'The specified post cannot be found.');
            }
        } else {
            throw new CHttpException(404, 'The specified post cannot be found.');
        }
    }

    /**
     * 
     */
    public function actionAdd() {
        $model = new Taxonomy();
        $listTaxonomy = Taxonomy::getListTaxonomy(1);
        if (isset($_POST['Taxonomy'])) {
            $model->attributes = $_POST['Taxonomy'];
            $parent = Taxonomy::model()->findByPk($_POST['Taxonomy']['parents']);
            if ($parent != null) {
                $image = CUploadedFile::getInstance($model, 'image');
                if ($image != null) {
                    $newName = time() . "." . $image->extensionName;
                    $model->image = $newName;
                }
                $result = $model->appendTo($parent);
                if ($result) {
                    if($image != null && isset($newName)){
                          $image->saveAs(PATH_IMAGE_PRODUCT . $newName);
                    }
                    Yii::app()->user->setFlash('success', "Danh mục sản phẩm đã được lưu");
                    $this->redirect(array('Taxonomy/index'));
                }
            }
        }
        $this->render('add', array('model' => $model, 'listTaxonomy' => $listTaxonomy));
    }
    
    /**
     * 
     */
    public function actionDelete($id = null){
         if ($id != null ) {
            $model = Taxonomy::model()->findByPk($id);
            if ($model != null) {
                if($model->deleteNode()){
                    Yii::app()->user->setFlash('success', "Chuyên mục đã được xóa");
                    $this->redirect(array('taxonomy/index'));
                }
            }else{
               throw new CHttpException(404, 'The specified post cannot be found.'); 
            }
         }else{
             throw new CHttpException(404, 'The specified post cannot be found.');
         }
    }

}